package com.instalinks.android.apps.smspostservice.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.instalinks.android.apps.smspostservice.helper.AppConfig;
import com.instalinks.android.apps.smspostservice.tasks.SmsPostingTask;
import com.instalinks.android.apps.smspostservice.tasks.UpdateConfigTask;
import com.instalinks.android.apps.smspostservice.tasks.WebSmsTask;
import com.instalinks.android.lib.LibConstants;

public class AppNotificationReceiver extends BroadcastReceiver {

	private Context mContext;
	private AppConfig mAppCfg;

	private String mode;
	private String appId;
	private String field1;
	private String field2;
	private String field3;
	private String field4;
	private String sender;
	private String text;

	@Override
	public void onReceive(Context context, Intent intent) {

		this.mContext = context;
		mAppCfg = new AppConfig(mContext);

		this.setValues(intent);

		if (intent.getAction().equals(
				LibConstants.IntentAction.NOTIFICATION_BROADCAST)
				&& appId.equalsIgnoreCase(mContext.getPackageName())) {
			handleNotification();
		} else if (intent.getAction().equals(
				LibConstants.IntentAction.SMS_BROADCAST)) {
			handleSmsInterception();
		}
	}

	private void setValues(Intent intent) {

		Bundle bundle = intent.getExtras();

		mode = bundle.getString(LibConstants.BroadcastFields.mode);
		appId = bundle.getString(LibConstants.BroadcastFields.appid);
		field1 = bundle.getString(LibConstants.BroadcastFields.field1);
		field2 = bundle.getString(LibConstants.BroadcastFields.field2);
		field3 = bundle.getString(LibConstants.BroadcastFields.field3);
		field4 = bundle.getString(LibConstants.BroadcastFields.field4);
		sender = bundle.getString(LibConstants.BroadcastFields.sender);
		text = bundle.getString(LibConstants.BroadcastFields.text);

		mode = (mode == null) ? "unknown" : mode;
		appId = (appId == null) ? "unknown" : appId;
		field1 = (field1 == null) ? "unknown" : field1;
		field2 = (field2 == null) ? "unknown" : field2;
		field3 = (field3 == null) ? "unknown" : field3;
		field4 = (field4 == null) ? "unknown" : field4;
		sender = (sender == null) ? "unknown" : sender;
		text = (text == null) ? "unknown" : text;
	}

	private void handleNotification() {
		if (mode.equalsIgnoreCase("system") && field3.equalsIgnoreCase("flash")) {
			mAppCfg.alert("System Flash");
		} else if (mode.equalsIgnoreCase("sms")) {
			if (mAppCfg.isWebSmsFlag() && mAppCfg.isSmsServiceRunning()) {
				new WebSmsTask(mContext, this.field1, this.field2).execute();
			}
		} else if (mode.equalsIgnoreCase("updateconfig")
				|| (mode.equalsIgnoreCase("system") && field3
						.equalsIgnoreCase("updateconfig"))) {

			new UpdateConfigTask(mContext).execute();
		}
	}

	private void handleSmsInterception() {
		if (mAppCfg.isSmsServiceRunning())
			new SmsPostingTask(mContext, sender, text).execute();
	}
}
